package com.spider.moudle.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * 查找符合条件的文件
 * @author Linbo Hu
 * @date 2018/1/12
 */
public class Search {

    public static void main(String[] args) {
        String path = "F:\\爬虫目录\\爱卡论坛\\speeches";
        Search search = new Search();
        search.search(path);
    }


    /**
     * 扫描文件夹中的文件(使用递归算法)
     * @param filePath
     */
    public void search(String filePath) {
        File root = new File(filePath);
        if(!root.exists()) {
            System.err.println(filePath + " 不存在！");
            return;
        }
        File[] files = root.listFiles();
        if(CommonUtil.isNotNull(files)) {
            if(files.length == 0) {
                System.err.println(filePath + " 下不存在文件");
                return;
            }
            for(File file : files) {
                if(file.isDirectory()) {
                    search(file.getAbsolutePath());
                } else {
                    if(file.getName().endsWith("bak")) {
                        return;
                    }
                    if(file.getName().endsWith(".zip")) {
                        if(file.delete()) {
                            System.out.println(file.getAbsolutePath() + "删除成功");
                        }
                    }
                    if(file.exists()) {
                        try {
                            read(file);
                            //System.out.println("success" + System.currentTimeMillis());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } else {
            System.out.println(root.getAbsolutePath() + "下不存在文件");
        }
    }

    /**
     * 从文件中读取数据
     * @param file
     */
    private void read(File file) throws Exception {
        InputStreamReader read = null;
        try {
            read = new InputStreamReader(new FileInputStream(file), "utf-8");
            BufferedReader bufferedReader = new BufferedReader(read);
            StringBuilder text = new StringBuilder();
            while (true) {
                String lineTxt = bufferedReader.readLine();
                if(lineTxt == null) {
                    break;
                } else {
                    text.append(lineTxt);
                }
            }
            if(CommonUtil.isNotNull(text)) {
                JSONObject jsonObject = JSON.parseObject(text.toString());
                JSONObject object = (JSONObject)jsonObject.get("data");
                JSONObject area = object.getJSONObject("area");
                if(object.containsKey("reply_nodes")) {
                    JSONArray reply_nodes = object.getJSONArray("reply_nodes");
                    for(int i = 0; i < reply_nodes.size(); i++) {
                        JSONObject reply_node = reply_nodes.getJSONObject(i);
                        JSONArray reply_reply_nodes = reply_node.getJSONArray("reply_nodes");
                        for(int j = 0; j < reply_reply_nodes.size(); j++) {
                            System.out.println(file.getAbsolutePath());
                        }
                    }
                } else {
                    System.out.println(file.getAbsolutePath());
                }

                if(area != null) {
                    System.out.println(file.getAbsolutePath());
                }
            }
        }  finally {
            if(read != null) {
                read.close();
            }
        }
    }
}
